/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#ifndef __SCRIPTINGENGINELUA_H__
#define __SCRIPTINGENGINELUA_H__

#pragma once

#include <map>
#include "IModule.h"

extern "C" {
#include "lua.h"
//#include "lauxlib.h"
//#include "lualib.h"

//#include <stdarg.h>
//#include <stdio.h>
//#include <stdlib.h>
}

#include "common/Singleton.h"

/**
 * \ingroup CoreEngine
 *
 *	A Class that implements some standard lua functionality 
 *
 * \date 04-17-2007
 *
 * \author Karl Moik
 *
 * \todo 
 *
 * \bug 
 *
 */
class DLL_EXPORT ScriptingEngineLua 
:	//public Singleton<ScriptingEngineLua>, 
	public IModule
{
public:
	ScriptingEngineLua(void);
	~ScriptingEngineLua(void);

	// Executes a string-command in a specific lua_State
	// Returns: Lua_Error code. 
	std::string processCommand(lua_State *L, const std::string &inputLine);
		
	// Loads a File into a Lua_State
	bool readFile(lua_State* L, const char* fileName);	

	// Writes a table of a Lua_State into a File 
	bool writeTableToFile(lua_State* L, const char* tableName, const char* fileName);

	// Proves if a key of a table has a specified value
	bool isTableValue(lua_State* L, const char* tableName, const char* key);
		
	// Returns a field of a table of type number
	lua_Number getFieldNum(lua_State* L, const char *tablename, const char *key);
		
	// Returns a field of a table of type const char*
	const char* getFieldString(lua_State* L, const char *tablename, const char *key);
		
	// Returns a field of a table of type bool
	bool getFieldBool(lua_State* L, const char *tablename, const char *key);	

	// Writes a field of a table of type number
	void setFieldNum(lua_State* L, const char* tableName, const char* key, lua_Number val);	

	// Writes a field of a table of type string
	void setFieldString(lua_State* L, const char* tableName, const char* key, std::string val);	

	// Writes a field of a table of type bool
	void setFieldBool(lua_State* L, const char* tableName, const char* key, bool val);	

	// Closes all lua_States inserted in mLua_States
	void Destroy();
	
	//Actually does nothing :-)
	//bool init();

private:
	
	// An error function
	void ScriptingEngineLua::error(const char *fmt, ...);
};

/**
 *  Initialize the Lua ScritpingEngine
 */
DLL_EXPORT
bool coreInitScriptingEngineLua();

DLL_EXPORT
ScriptingEngineLua* coreGetScriptingEngineLua();

#endif // __SCRIPTINGENGINELUA_H__